package com.jiantong.news.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.news.impl.dao.NewsDao;
import com.jiantong.news.model.News;
import com.jiantong.news.service.NewsWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 首页\新闻
 * Created by mz03 on 2017/3/15.
 *zyp
 */
@Slf4j
@Service
@RpcProvider
public class NewsWriteServiceImpl implements NewsWriteService{

    private final NewsDao newsDao;

    @Autowired
    public NewsWriteServiceImpl(NewsDao newsDao) {
        this.newsDao = newsDao;
    }

    /**
     * 创建
     * @param news 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(News news) {

        try {
            newsDao.create(news);
            return Response.ok(news.getId());
        }catch (Exception e) {
            log.error("failed.to.create.news:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.failed");
        }
    }

    /**
     * 更新
     * @param news 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(News news) {

        try{
            //添加成功
            return Response.ok(newsDao.update(news));
        }catch (Exception e){
            //添加过程出现异常
            log.error("eeror :{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.failed");
        }
    }
}
